﻿using IOP.SgrA;
using IOP.SgrA.ECS;
using IOP.SgrA.SilkNet.Vulkan;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.IO;
using System.Numerics;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using IOP.Extension.DependencyInjection;
using IOP.ISEA;
using Silk.NET.Vulkan;

namespace VkSample114;

[ModulePriority(ModulePriority.Assets)]
public class AssetsModule : VulkanModule, IVulkanRenderWapperModule
{
    public VulkanRenderWapper RenderWapper { get; set; }
    [Autowired]
    private ILogger<AssetsModule>? _Logger { get; set; }

    protected override async Task Load(VulkanGraphicsManager graphicsManager)
    {
        try
        {
            var basePath = AppContext.BaseDirectory;
            var modelsPath = Path.Combine(basePath, "Assets", "Models");
            var texPath = Path.Combine(basePath, "Assets", "Textures");
            var lDevice = graphicsManager.VulkanDevice;

            ISEADocument document = new();
            await document.Load(Path.Combine(AppContext.BaseDirectory, "test114.isea"));
            var render = await graphicsManager.CreateRenderObjectsFromISEA<RenderObject>(document, "Eagle");

            graphicsManager.TryGetRenderGroup("Common", out IRenderGroup common);
            var aTex = graphicsManager.CreateUniformBufferTexture("uBfb", 4, setIndex: 0, binding: 0);
            render.AddComponent(aTex);
            render.CreateAnimation<RenderObject, KeyFrameAnimation>(nameof(KeyFrameAnimation));

            var commonArchetype = ContextManager.CreateArchetype("Common", new TransformComponent());
            ContextManager.CreateContexts(1, commonArchetype, common, render);
        }
        catch (Exception e)
        {
            _Logger?.LogError(e.Message + "\r\n" + e.StackTrace);
        }
    }

    protected override Task Unload(VulkanGraphicsManager manager)
    {
        return Task.CompletedTask;
    }
}
